package com.ss.android.article.browser.main;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.a.a.NightMode;
import com.a.a.c;
import com.a.a.e;
import com.a.a.s;
import com.a.a.t;
import com.a.a.w;
import com.a.a.x;
import com.a.a.y;
import com.ss.android.article.browser.BrowserPage;
import com.ss.android.article.browser.FeedFragment;
import com.ss.android.article.browser.c.SpecialPageHelper;
import com.ss.android.article.browser.c.d;
import com.ss.android.article.browser.constant.b;
import com.ss.android.article.browser.constant.g;
import com.ss.android.article.browser.constant.j;
import com.ss.android.article.browser.constant.k;
import com.ss.android.article.browser.preference.PreferenceManager;
import com.ss.android.common.app.AbsApplication;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TabInspector {
    public static final String TAG = TabInspector.class.getSimpleName();
    static TabInspector sInstance;
    @Nullable
    private static WeakReference<OnTabNumberChangedListener> sRefOnTabNumberChangedListener;
    public final BrowserPage mBrowserPage;
    @NonNull
    private final List<Runnable> h;
    PreferenceManager mPreferenceManager;
    Application mApplication;
    @NonNull
    private List<FeedFragment> mTabList;
    @Nullable
    private FeedFragment mCurrentFragment;
    private boolean j;

    private TabInspector(BrowserPage browserPage) {
        this.mTabList = new ArrayList(1);
        this.h = new ArrayList();
        this.j = false;
        this.mBrowserPage = browserPage;
        this.mApplication = AbsApplication.getApplication();
        this.mPreferenceManager = PreferenceManager.getInstance();
    }

    public static com.a.a.a a(@NonNull BrowserPage arg2, @Nullable Intent arg3) {
        if (TabInspector.sInstance == null) {
            Class v1 = TabInspector.class;
            synchronized (v1) {
                if (TabInspector.sInstance == null) {
                    TabInspector.sInstance = new TabInspector(arg2);
                }
            }
        }

        return TabInspector.sInstance.a(arg3);
    }

    public static TabInspector getInstance() {
        return TabInspector.sInstance;
    }

    public static void setOnTabNumberChangedListener(@Nullable OnTabNumberChangedListener onTabNumberChangedListener) {
        TabInspector.sRefOnTabNumberChangedListener = new WeakReference(onTabNumberChangedListener);
    }

    static void a(TabInspector arg1) {
        arg1.mCurrentFragment = null;
    }

    void a(TabInspector arg2, final String arg3, final Context context, final com.a.a.c arg5) {
        NightMode v0 = new NightMode() {

            public final void f(@NonNull Object arg5) {
                Bundle bundle = d.readBundleFromStorage(TabInspector.this.mApplication, "SAVED_TABS.parcel");
                if (bundle != null) {
                    Log.d(TabInspector.TAG, "Restoring previous WebView state now");
                    Iterator<String> iterator = bundle.keySet().iterator();
                    while (iterator.hasNext()) {
                        String key = iterator.next();
                        if (!key.startsWith("WEBVIEW_")) {
                            continue;
                        }
                        ((y) arg5).onNext(bundle.getBundle(key));
                    }
                }
                d.deleteFile(TabInspector.this.mApplication, "SAVED_TABS.parcel");
                ((y) arg5).onComplete();
            }
        };
        android.support.design.a.b(v0);
        w v1 = new w(((NightMode) v0));
        v1.b = android.support.design.a.c();
        v1.c = android.support.design.a.b();
        v1.a(new x() {

            public final void a() {
                if (!TextUtils.isEmpty(arg3)) {
                    if (TabInspector.c(TabInspector.this).isEmpty()) {
                        TabInspector.this.a(arg3);
                    }

                    TabInspector.b(TabInspector.this);
                    arg5.onComplete();
                } else {
                    if (TabInspector.c(TabInspector.this).isEmpty()) {
                        TabInspector.this.a((String) null);
                    }

                    TabInspector.b(TabInspector.this);
                    arg5.onComplete();
                }
            }

            public final void a(@Nullable Object arg4) {
                s v1_1;
                final FeedFragment v0 = TabInspector.this.a("");
                String v1 = ((Bundle) arg4).getString("URL_KEY");
                if (v1 == null || v0.d().getWebView() == null) {
                    if (v0.d().getWebView() != null) {
                        v0.d().getWebView().restoreState(((Bundle) arg4));
                    }
                } else if (SpecialPageHelper.isBookmarksUrl(v1)) {
                    v1_1 = s.a(new com.ss.android.article.browser.constant.c(new b(context)));
                    ((e) v1_1).b = android.support.design.a.c();
                    ((e) v1_1).c = android.support.design.a.b();
                    v1_1.a(new t() {

                        public final void a(@Nullable Object arg2) {
                            v0.loadUrl(((String) arg2));
                        }
                    });
                } else if (SpecialPageHelper.isDownloadsUrl(v1)) {
                    v1_1 = s.a(new com.ss.android.article.browser.constant.h(new g()));
                    ((e) v1_1).b = android.support.design.a.c();
                    ((e) v1_1).c = android.support.design.a.b();
                    v1_1.a(new t() {

                        public final void a(@Nullable Object arg2) {
                            v0.loadUrl(((String) arg2));
                        }
                    });
                } else if (SpecialPageHelper.isHomePageUrl(v1)) {
                    v0.d().loadUrl(v1);
                } else if (SpecialPageHelper.isHistoryUrl(v1)) {
                    v1_1 = s.a(new k(new j()));
                    ((e) v1_1).b = android.support.design.a.c();
                    ((e) v1_1).c = android.support.design.a.b();
                    v1_1.a(new t() {

                        public final void a(@Nullable Object arg2) {
                            v0.loadUrl(((String) arg2));
                        }
                    });
                }
            }
        });
    }

    static void b(TabInspector arg2) {
        arg2.j = true;
        Iterator<Runnable> v1 = arg2.h.iterator();
        while (v1.hasNext()) {
            v1.next().run();
        }
    }

    static List c(TabInspector arg1) {
        return arg1.mTabList;
    }

    public synchronized final void a(@NonNull Runnable arg2) {
        if (this.j) {
            arg2.run();
        } else {
            this.h.add(arg2);
        }
    }

    public synchronized final int a(FeedFragment arg2) {
        return this.mTabList.indexOf(arg2);
    }

    @Nullable
    public synchronized final FeedFragment a(int arg4) {
        Iterator<FeedFragment> iterator = this.mTabList.iterator();
        do {
            if (iterator.hasNext()) {
                FeedFragment feedFragment = iterator.next();
                if (feedFragment.d() == null) {
                    continue;
                }

                if (feedFragment.d().getWebView() == null) {
                    continue;
                }

                if (feedFragment.d().getWebView().hashCode() != arg4) {
                    continue;
                }
                return feedFragment;
            } else {
                break;
            }
        } while (true);
        return null;

    }

    @NonNull
    private synchronized com.a.a.a a(@Nullable final Intent intent) {
        return com.a.a.a.a(new NightMode() {

            public final void f(@NonNull Object arg6) {
                String v1 = null;
                TabInspector.this.d();
                String url = intent != null ? intent.getDataString() : v1;
                Log.d(TabInspector.TAG, "URL from intent: " + url);
                TabInspector.a(TabInspector.this);
                if (TabInspector.this.mPreferenceManager.mSharedPreferences.getBoolean("restoreclosed", true)) {
                    TabInspector.this.a(TabInspector.this, url, TabInspector.this.mApplication, ((com.a.a.c) arg6));
                } else {
                    if (!TextUtils.isEmpty(url)) {
                        TabInspector.this.a(url);
                    } else {
                        TabInspector.this.a(v1);
                    }

                    TabInspector.b(TabInspector.this);
                    ((c) arg6).onComplete();
                }
            }
        });
    }

    @NonNull
    public synchronized final FeedFragment a(@Nullable String arg2) {
        return this.b(arg2);
    }

    @Nullable
    public synchronized final FeedFragment switchToTab(int position) {
        Log.d(TabInspector.TAG, "switch to tab: " + position);
        if (position < 0 || position >= this.mTabList.size()) {
            Log.e(TabInspector.TAG, "Returning a null LightningView requested for position: " + position);
            return null;
        } else {
            FeedFragment feedFragment = this.mTabList.get(position);
            if (feedFragment != null) {
                this.mCurrentFragment = feedFragment;
            }
            return feedFragment;
        }
    }

    @Nullable
    public synchronized final FeedFragment getCurrentFragment() {
        return this.mCurrentFragment;
    }

    @NonNull
    private synchronized FeedFragment b(@Nullable String url) {
        Bundle bundle = new Bundle();
        if (TextUtils.equals("http://lemon_browser_setting", url)) {
            url = "";
        }

        bundle.putString("bundle_url", url);
        bundle.putBoolean("bundle_isIncognito", false);
        FeedFragment feedFragment;
        if (com.ss.android.article.browser.setting.a.a()) {
            com.ss.android.article.browser.website.a tab = com.ss.android.article.browser.website.a.b(bundle);
            this.mTabList.add(tab);
            feedFragment = tab;
        } else {
            HomePageFragment v0_3 = HomePageFragment.newHomePageFragment(bundle);
            this.mTabList.add(v0_3);
            feedFragment = v0_3;
        }

        if (TabInspector.sRefOnTabNumberChangedListener != null) {
            Object v0_4 = TabInspector.sRefOnTabNumberChangedListener.get();
            if (v0_4 != null) {
                ((OnTabNumberChangedListener) v0_4).tabNumberChanged(this.tabSize());
            }
        }
        return feedFragment;
    }

    public synchronized final int tabSize() {
        return this.mTabList.size();
    }

    public synchronized final void d() {
        Iterator<FeedFragment> iterator = this.mTabList.iterator();
        while (iterator.hasNext()) {
            iterator.next().e();
        }
        this.mTabList.clear();
        this.j = false;
        this.mCurrentFragment = null;
    }

    public synchronized final int e() {
        return this.mTabList.size() - 1;
    }

    @Deprecated
    public interface OnTabNumberChangedListener {
        void tabNumberChanged(int tabNumber);
    }
}

